
//
//  WZPictureView.swift
//  weibo
//
//  Created by mac on 16/3/2.
//  Copyright © 2016年 mac. All rights reserved.
/*Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist: <NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}'*/

import UIKit


let KcellWithReuseIdentifier = "CellWithReuseIdentifier"
let KitemMargin = CGFloat(10)
let KitemWH = (UIScreen.mainScreen().bounds.width - CGFloat(4) * KitemMargin)/CGFloat(3)

class WZPictureView: UICollectionView,UICollectionViewDataSource,UICollectionViewDelegate{

    
    var pictureList: [WZCellViewModel]?{
        didSet{
            
//            设置collectionview的约束
            let size = getCollectionViewSize()
            self.snp_makeConstraints { (make) -> Void in
                make.width.equalTo(size.width)
                make.height.equalTo(size.height)
            }
            
            reloadData()
        }
    }
//    定义flowlayout属性 不要加lazy
    var flowLayout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
       
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func getCollectionViewSize()->CGSize{
        let count = pictureList?.count
        
        if count <= 3{
            
            return CGSizeMake((KitemMargin + KitemWH) * CGFloat(3) - CGFloat(KitemMargin), KitemWH)
            
        }else if count == 4{
            return CGSizeMake((KitemMargin + KitemWH) * CGFloat(2) - CGFloat(KitemMargin), KitemWH * CGFloat(2) + KitemMargin)
        }else{
            let row = (count! - 1) / 3 + 1
            return CGSizeMake((KitemMargin + KitemWH) * CGFloat(3) - CGFloat(KitemMargin), (KitemMargin + KitemWH) * CGFloat(row))
        }
    }
    
    
    
    private func setupUI(){
        self.delegate = self
        self.dataSource = self
        backgroundColor = UIColor.whiteColor()
//        注册cell
    registerClass(pictureCell.self, forCellWithReuseIdentifier: KcellWithReuseIdentifier)
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        flowLayout.itemSize = CGSizeMake(KitemWH, KitemWH)
        flowLayout.minimumInteritemSpacing = KitemMargin
        flowLayout.minimumLineSpacing = KitemMargin
        
    }
  
    
}
extension WZPictureView {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return pictureList?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(KcellWithReuseIdentifier, forIndexPath: indexPath) as! pictureCell
       
        
        cell.pictureViewModel = pictureList![indexPath.item].pictureViewModel
        
        return cell
    }
}


class pictureCell: UICollectionViewCell {
    var pictureViewModel: WZPictureModel?{
        
        didSet{
            
            
            let url = NSURL(string: pictureViewModel?.thumbnail_pic ?? "")
            
            if let Ourl = url {
                
                iconImageView.sd_setImageWithURL(Ourl)
            }
            
        }
        
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var iconImageView:UIImageView = {
       let imageView = UIImageView()
        return imageView
    }()
    
    
    private  func setupUI(){
        iconImageView.frame = self.bounds
        addSubview(iconImageView)
    }
    
}


